package com.zzptc.hjp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.zzptc.hjp.databinding.ActivityMainBinding;
import com.zzptc.hjp.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    boolean flag;
    private static final String TAG = "MainActivity";
    List<User>list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        event();
        User user1=new User("admian","123");
        User user2=new User("admian","123");
        User user3=new User("admian","123");
        list.add(user1);
        list.add(user2);
        list.add(user3);

    }
    public void event(){
        //自动登录是否被改变
        binding.switchLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                 flag=true;
                }else{
                 flag=false;
                }
                Toast.makeText(MainActivity.this, ""+flag, Toast.LENGTH_SHORT).show();
            }
        });
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取用户名和密码的值
                String name = binding.etName.getText().toString().trim();
                String pwd = binding.etPwd.getText().toString().trim();
                SharedPreferences sh=getSharedPreferences("one",MODE_PRIVATE);
                SharedPreferences.Editor editor =sh.edit();
                if(flag){
                    //记住(用户名，密码，是否自动登录)
                    editor.putString("uname",name);
                    editor.putString("password",pwd);
                    editor.putBoolean("autologin",false);
                    editor.apply();
                }else{
                    editor.putString("uname","");
                    editor.putString("passwor","");
                    editor.putBoolean("autologin",false);
                    editor.apply();
                }
                //登录
                login(name,pwd);


            }
        });

        //注册事件
        binding.btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Toast.makeText(MainActivity.this, "333333", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });

    }
    public void login(String name,String pwd){
    boolean isLogin=false;
    for(User u:list){
      if(u.getUsername().equals(name)&&u.getPassword().equals(pwd)){
          Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
          isLogin=true;
      }else{
          isLogin=false;
      }
    }
    if(isLogin){
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==RESULT_OK){
            //取值
            String uname =  data.getStringExtra("uname");
            String pwd=data.getStringExtra("pwd");
            Log.d(TAG,"回传的值是："+uname);
            Log.d(TAG,"回传的值是："+pwd);
            //封装数据
            User user=new User(uname,pwd);

            //注册
            reg(user);


        }

    }

    private void reg(User user) {
        // 判断集合中有没有对象或有没有该对象
        if (list.indexOf(user) == -1) {
            list.add(user);
        }else {
            Toast.makeText(this, "该用户已存在", Toast.LENGTH_SHORT).show();
        }
    }

}
